package cn.demomaster.quickui_sample.view.picktime;

import android.content.Context;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;

import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.demomaster.quickcompat.widget.base.QuickGravity;
import com.demomaster.quickcompat.widget.dialog.QuickDialog;

import java.util.List;

import cn.demomaster.quickui_sample.R;


/**
 * @author squirrel桓
 * @date 2018/11/16.
 * description：
 */
public class LanguagePickerPop extends QuickDialog implements View.OnClickListener {

    private Button cancelBtn;
    private Button confirmBtn;

    private RecyclerView recyclerView;
    private LanguageAdapter languageAdapter;
    List<LanguageAdapter.LanguageModel> languageModels;

    public LanguagePickerPop(Context context, Builder builder) {
        super(context, builder);
        this.languageModels = builder.date;
        this.initView();
    }

    private void initView() {
        this.cancelBtn = findViewById(R.id.btn_cancel);
        this.confirmBtn = findViewById(R.id.btn_confirm);
        //this.pickerContainerV = findViewById(R.id.container_picker);

        recyclerView = findViewById(R.id.recyclerView);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        //设置布局管理器
        recyclerView.setLayoutManager(layoutManager);
        //使用网格布局展示
        //recyclerView.setLayoutManager(new GridLayoutManager(mContext, 3));
        //设置为垂直布局，这也是默认的
        layoutManager.setOrientation(RecyclerView.VERTICAL);
        languageAdapter = new LanguageAdapter(getContext(), languageModels);
        languageAdapter.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //(view, position, languageModel) -> languageModels.set(position,languageModel)
            }
        });
        //设置Adapter
        recyclerView.setAdapter(languageAdapter);
        //设置分隔线
        //recyclerView.addItemDecoration( new DividerGridItemDecoration(this ));
        //设置分割线使用的divider
        //recyclerView.addItemDecoration(new android.support.v7.widget.DividerItemDecoration(mContext, android.support.v7.widget.DividerItemDecoration.VERTICAL));
        //设置增加或删除条目的动画
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        this.cancelBtn.setOnClickListener(this);
        this.confirmBtn.setOnClickListener(this);
    }

    public void onClick(View v) {
        dismiss();
    }

    public static class Builder<T extends QuickDialog.Builder> extends QuickDialog.Builder<T> {
        private List<LanguageAdapter.LanguageModel> date;

        public Builder(Context context) {
            super(context);
            setContentView(R.layout.layout_language_picker);
            cancelable = true;
            isFullScreen=true;
            animationStyleID = cn.demomaster.quickui.R.style.keybored_anim;
            quickGravity = QuickGravity.BOTTOM;
            this.enablePadding = false;
        }

        public Builder setDate(List<LanguageAdapter.LanguageModel> date) {
            this.date = date;
            return this;
        }

        public LanguagePickerPop build() {
            return new LanguagePickerPop(context, this);
        }

        public Builder setDefaultValue(String def_provience) {
            //this.def_value = def_provience;
            return this;
        }
    }

}
